package com.ruoyi.app.service.impl;

import com.alibaba.fastjson2.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.app.domain.AppUser;
import com.ruoyi.app.domain.HtCompareRecord;
import com.ruoyi.app.mapper.AppUserMapper;
import com.ruoyi.app.mapper.HtCompareRecordMapper;
import com.ruoyi.app.service.BaiDuAiService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.translate.domain.HtImport;
import com.ruoyi.translate.domain.ShiBieVo;
import com.ruoyi.translate.domain.WordVo;
import com.ruoyi.translate.domain.Words;
import com.ruoyi.translate.mapper.HtImportMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.annotation.ElementType;
import java.util.*;

/**
 * @ProjectName: translate
 * @Package: com.ruoyi.app.service.impl
 * @ClassName: BaiDuAiServiceImpl
 * @Author: CJ
 * @Description:
 * @CreateTime: 2025-02-07 10:49
 */
@Service
public class BaiDuAiServiceImpl implements BaiDuAiService {
    @Autowired
    private HtImportMapper htImportMapper;
    @Autowired
    private HtCompareRecordMapper htCompareRecordMapper;
    @Autowired
    private AppUserMapper appUserMapper;
    @Override
    @Transactional
    public AjaxResult getNewList(List<Words> input,Integer userId) {

        List<WordVo> result = new ArrayList<>();
        // 查询最新的导入数据
        ShiBieVo list = htImportMapper.selectNewData();
        // 数据类型转换
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            List<HtImport> htImports = objectMapper.readValue(list.getWords(), new TypeReference<List<HtImport>>() {
            });

            for (int i = 0; i < htImports.size(); i++) {
                // 判读是否input是否还有值
                if (input.size() <= i) {
                    result.add(new WordVo("",htImports.get(i).getWords(),0));
                }else {
                    // 判读是否相等
                    if (htImports.get(i).getWords().equals(input.get(i).getWords())) {
                        result.add(new WordVo(input.get(i).getWords(),htImports.get(i).getWords(),1));
                    }else {
                        result.add(new WordVo(input.get(i).getWords(),htImports.get(i).getWords(),0));
                    }
                }
            }

            // 获取用户信息
            AppUser appUser = appUserMapper.selectAppUserById(Long.valueOf(userId));
            // todo 可能需要写入记录
            HtCompareRecord htCompareRecord = new HtCompareRecord();
            htCompareRecord.setUserId(appUser.getId());
            htCompareRecord.setTitle(list.getTitle());
            htCompareRecord.setCompareText(list.getWords());
            htCompareRecord.setOriginalText(JSON.toJSONString(input));
            htCompareRecord.setDiffResult(JSON.toJSONString(result));
            htCompareRecord.setCreateBy(appUser.getNickName());
            htCompareRecord.setCreateTime(new Date());

            htCompareRecordMapper.insertHtCompareRecord(htCompareRecord);
            return AjaxResult.success(result);

        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
